//
//  DDOriginalView.swift
//  swift微博
//
//  Created by whitcast on 16/7/14.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class DDOriginalView: UIView {
    //间距
     let margin = 10
    //原创的底部约束
    var selfViewBottomConstraint: Constraint?
    //viewModel数据
    var statuesViewModel :DDStatuesViewModel?{
        didSet{
            if let url = statuesViewModel?.statuesModel?.user?.profile_image_url {
                //赋值头像
                headImgView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "avatar_default"))
            }
            //名字
            nameLab.text = statuesViewModel?.statuesModel?.user?.screen_name
            //会员等级
            huiYuanImg.image = statuesViewModel?.imgOfMB
            //时间
            timeLab.text = statuesViewModel?.created_atTimeText
            //来源
            sourselab.text = "来自: " + (statuesViewModel?.getSourse())!
            //微博内容
            contentLab.text = statuesViewModel?.statuesModel?.text
            //时间标签
            
            
            
            //认证图片
            renZhengImg.image = statuesViewModel?.verified_typeImg
            //原创图片视图
            picViews.pic_urlsArr = statuesViewModel?.statuesModel?.pic_urls
            //卸载约束
            selfViewBottomConstraint?.uninstall()
            
            if statuesViewModel?.statuesModel?.pic_urls?.count > 0 {
                //不隐藏视图
                picViews.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.selfViewBottomConstraint = make.bottom.equalTo(picViews).offset(margin).constraint
                })
            }else {
                //隐藏视图
                picViews.hidden = true
                //跟新底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.selfViewBottomConstraint = make.bottom.equalTo(contentLab.snp_bottom).offset(margin).constraint
                })
                
            }
        }
    }
    //头像
    lazy var headImgView: UIImageView = UIImageView()
    //名称
    lazy var nameLab: UILabel = {
        let lab = UILabel()
        
        lab.font = UIFont.systemFontOfSize(15)
        
        return lab
    }()
    //会员标识
    lazy var huiYuanImg: UIImageView = {
    
        let imgView = UIImageView()
        
        return imgView
    
    }()
    //时间标签
    lazy var timeLab: UILabel = {
    let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(13)
        return lab
    }()
   //来源
    lazy var sourselab: UILabel = {
        let lab = UILabel()

        lab.font = UIFont.systemFontOfSize(13)
        return lab
    }()
    //认证图片
    lazy var renZhengImg: UIImageView = UIImageView()
    //微博内容
    lazy var contentLab: UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.font = UIFont.systemFontOfSize(15)
        return lab
    }()
    //原创图片
    lazy var picViews: DDPIcView = {
       
        let picView = DDPIcView()
        
        return picView
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //创建控件,设置约束
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建控件,设置约束
    func setUpUI() {
        //添加进view
        addSubview(headImgView)
        addSubview(nameLab)
        addSubview(huiYuanImg)
        addSubview(timeLab)
        addSubview(sourselab)
        addSubview(contentLab)
        addSubview(renZhengImg)
        addSubview(picViews)
        
        //设置约束
        
       
        headImgView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(margin)
            make.leading.equalTo(self.snp_leading).offset(margin)
                }
        renZhengImg.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImgView.snp_trailing)
            make.centerY.equalTo(headImgView.snp_bottom)
        }
        nameLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView.snp_trailing).offset(margin)
            make.top.equalTo(headImgView)
        }
        huiYuanImg.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLab.snp_top)
            make.leading.equalTo(nameLab.snp_trailing)
        }
        timeLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLab.snp_leading)
            make.top.equalTo(nameLab.snp_bottom).offset(margin)
        }
        sourselab.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLab.snp_top)
            make.leading.equalTo(timeLab.snp_trailing).offset(margin)
        }
        contentLab.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImgView.snp_leading)
            make.width.equalTo(self.snp_width).offset(-margin)
            make.top.equalTo(headImgView.snp_bottom).offset(margin)
        }
        picViews.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLab.snp_bottom).offset(margin)
            make.leading.equalTo(contentLab)
            
        }
        //设置self的高度
        self.snp_makeConstraints { (make) -> Void in

            self.selfViewBottomConstraint = make.bottom.equalTo(picViews.snp_bottom).offset(margin).constraint
            
        }

    }
    
}
